package com.cheyun.netsalev3.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.cheyun.netsalev3.BuildConfig;
import com.cheyun.netsalev3.R;

import java.util.List;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

/**
 * @author Michael
 * @date 2018/11/16 - 16:22.
 */
public class MyContentBehavior extends HeaderScrollingViewBehavior {

    public MyContentBehavior() {
    }

    public MyContentBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
//        TypedArray a = context.obtainStyledAttributes(attrs, com.google.android.material.R.styleable.ScrollingViewBehavior_Layout);
//        this.setOverlayTop(a.getDimensionPixelSize(com.google.android.material.R.styleable.ScrollingViewBehavior_Layout_behavior_overlapTop, 0));
//        a.recycle();
    }

    @Override
    View findFirstDependency(List<View> views) {
        int i = 0;
        for (int z = views.size(); i < z; ++i) {
            View view =  views.get(i);
            if (isDependOn(view)) {
                return  view;
            }
        }
        return null;
    }

    //依赖的判断
    private boolean isDependOn(View dependency) {
        return dependency != null && dependency.getId() == R.id.recyclerViewTop;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return isDependOn(dependency);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if (BuildConfig.DEBUG) {
            Log.d("", "onDependentViewChanged");
        }
        offsetChildAsNeeded(parent, child, dependency);
        return false;
    }

    private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) {
        child.setTranslationY((int) (dependency.getTranslationY()*2 / (getScrollRange(dependency) * 1.0f) * getScrollRange(dependency)));
    }




}
